package com.scu.qingshi.infrastructure.dao.po;

import com.baomidou.mybatisplus.annotation.IdType;

import java.util.*;

import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.Accessors;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.tuple.MutablePair;
import org.apache.commons.lang3.tuple.Pair;

/**
 * <p>
 * 
 * </p>
 *
 * @author 青时
 * @since 2022-03-06
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value="面试流程", description="")
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class InterviewProcess implements Serializable {

    private static final long serialVersionUID=1L;

    @TableId(value = "inter_id", type = IdType.AUTO)
    private Integer interId;

    private String name;

    private String nodes;

    private Integer nodeCount;

    private Date createTime;

    private Date modifyTime;

    // 暂时不用
    @ApiModelProperty(value = "0表示报名未截止 1表示报名截止")
    private Integer end;

    @ApiModelProperty(value = "人数限制")
    private Integer personLimit;

    @ApiModelProperty(value = "0为不发布 1为已发布")
    private Integer publish;

    @ApiModelProperty(value = "面试内容")
    private String interContent;

    private String openId;

    @ApiModelProperty(value = "报名截止日期")
    private Date deadline;

    public void addNewNode(Integer nodeId){

        if (StringUtils.isBlank(nodes)){
            nodes = String.valueOf(nodeId);
        }else {
            nodes+="-" + nodeId;
        }

    }

    public boolean hasPublish(){
        return publish==1;
    }

    public boolean hasComplete(){
        if(Objects.isNull(nodes)){
            return false;
        }
        return nodes.split("-").length == nodeCount;
    }

    public Pair<Boolean,Integer> getFirstNodeId(){
        if (Objects.isNull(nodes)){
            return new MutablePair<>(false,-1);
        }
        return new MutablePair<>(true,Integer.valueOf(nodes.split("-")[0]));
    }

    public Pair<Boolean,Integer> getLastNodeId() {
        if (Objects.isNull(nodes)){
            return new MutablePair<>(false,-1);
        }
        String[] split = nodes.split("-");
        return new MutablePair<>(true,Integer.valueOf(split[split.length-1]));
    }

    public String endVO(){
        return end == 0 ? "报名未截止" : "报名已截止";
    }

    public String publishVO(){
        return publish == 0 ? "已下架" : "报名仍在进行";
    }

    public List<Integer> canViewNodes(Integer curNodeId){
        String cur = String.valueOf(curNodeId);
        List<Integer> ids = new ArrayList<>();
        String[] nodeIds = nodes.split("-");
        for (String nodeId : nodeIds) {
            ids.add(Integer.valueOf(nodeId));
            if (Objects.equals(nodeId,cur)){
                break;
            }
        }
        return ids;
    }

    public List<Integer> getNodeList(){
        if (StringUtils.isBlank(nodes)){
            return Collections.emptyList();
        }
        List<Integer> ids = new ArrayList<>();
        String[] nodeIds = nodes.split("-");
        for (String nodeId : nodeIds) {
            ids.add(Integer.valueOf(nodeId));
        }
        return ids;
    }

    public void publish(){
        this.publish = 1;
    }

    public Integer passCurNode(String curId){
        Integer res = -1;
        String[] nodeIds = nodes.split("-");
        for (int i = 0; i < nodeIds.length; i++) {
            if (nodeIds[i].equals(curId)){
                res = Integer.parseInt(nodeIds[i+1]);
            }
        }

        return res;
    }

    public void deleteNode(Integer nodeId) {
        List<String> arr = new ArrayList<>(Arrays.asList(nodes.split("-")));
        arr.remove(String.valueOf(nodeId));
        nodes = "";
        for (String s : arr){
            if (StringUtils.isBlank(nodes)){
                nodes+=s;
            }else {
                nodes+="-"+s;
            }
        }
    }

    public void endProcess() {
        end = 1;
    }

    public boolean hasCompete(){
        if (StringUtils.isBlank(nodes)){
            return false;
        }else {
            return nodes.split("-").length == nodeCount;
        }
    }

    public Integer getState(){

        if (hasPublish()){
            return 1;
        }else if (hasCompete()){
            return 0;
        }else {
            return 2;
        }
    }

    public Integer getCurNodeCount(){
        if (StringUtils.isBlank(nodes)){
            return 0;
        }else {
            return nodes.split("-").length;
        }
    }

    public void soldOut() {
        setPublish(0);
    }

    public Integer curRound(Integer nodeId){
        String[] split = nodes.split("-");
        for (int i = 0; i < split.length; i++) {
            if (String.valueOf(nodeId).equals(split[i])) {
                return i+1;
            }
        }
        return -1;
    }

    public Pair<Boolean,Integer> preNode(Integer nodeId) {
        String[] l = nodes.split("-");
        if (l.length <= 1){
            return Pair.of(Boolean.FALSE,-1);
        }
        for (int i = 1; i < l.length; i++) {
            if (l[i].equals(String.valueOf(nodeId))){
                return Pair.of(Boolean.TRUE,Integer.valueOf(l[i-1]));
            }
        }
        return Pair.of(Boolean.FALSE,-1);
    }
}
